package com.bruce.algorithm.leetcode.queue;

import java.util.Stack;

/**
 * @author luotuan
 * @Description 232. 用栈实现队列
 * @url https://leetcode-cn.com/problems/implement-queue-using-stacks/submissions/
 * @create 2020-08-06 19:57
 **/
public class StackToQueue {
    private Stack<Integer> inStack;
    private Stack<Integer> outStack;

    /** Initialize your data structure here. */
    public StackToQueue() {
        inStack = new Stack<>();
        outStack = new Stack<>();
    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        inStack.push(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        if(outStack.isEmpty()){
            int size = inStack.size();
            for(int i=0;i<size;i++){
                int value = inStack.pop();
                outStack.push(value);
            }
        }
        return outStack.pop();
    }

    /** Get the front element. */
    public int peek() {
        if(outStack.isEmpty()){
            int size = inStack.size();
            for(int i=0;i<size;i++){
                int value = inStack.pop();
                outStack.push(value);
            }
        }
        return outStack.peek();
    }

    /** Returns whether the queue is empty. */
    public boolean empty() {
        return inStack.isEmpty() && outStack.isEmpty();
    }
}
